package com.itboye.ihomebank.activity.myzhujia;

import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.MyWalletBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.NetPublicConstant;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.activity.me.ActivityZhangHu;
import com.itboye.ihomebank.activity.myzhujia.qianbao.ActivityChongZhi;
import com.itboye.ihomebank.activity.myzhujia.qianbao.ActivityTiXian;
import com.itboye.ihomebank.activity.myzhujia.qianbao.ActivityZhiFuPass;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.web.WebActivity;

import java.util.Observable;
import java.util.Observer;

/**
 * 我的钱包
 * 
 */
public class ActivityMyWallte extends BaseOtherActivity implements Observer {
	View v_statusbar;
	TextView add_shap_title_tv, save;
	ImageView close_icon;
	RelativeLayout qianbao_chongzhi, qianbao_tixian, qianbao_mingxi,
			qianbao_bangding;

	TextView qianbao_money;// 账户余额
	UserPresenter userPresenter;
	private String uid;
	private String password;

	public static ActivityMyWallte context;

	public static ActivityMyWallte getInstance() {
		return context;
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.save:// 设置支付密码
			intent = new Intent(this, ActivityZhiFuPass.class);
			startActivity(intent);

			break;
		case R.id.qianbao_chongzhi:// 充值
			intent = new Intent(this, ActivityChongZhi.class);
			startActivity(intent);
			break;
		case R.id.qianbao_tixian:// 提现
			intent = new Intent(this, ActivityTiXian.class);
			startActivity(intent);
			break;
		case R.id.qianbao_mingxi:// 收支明细

			intent = new Intent(this, WebActivity.class);
			intent.putExtra("url", NetPublicConstant.WEB_URL
					+ "/wallet/balance?uid=" + uid + "&psw="
					+ password);
			intent.putExtra("title", "收支明细");
			startActivity(intent);
			break;
		case R.id.qianbao_bangding:// 账户绑定
			intent = new Intent(this, ActivityZhangHu.class);
			startActivity(intent);
			break;
		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_mywallet;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		setStatusBarHeight(v_statusbar);
		context = this;
		add_shap_title_tv.setText("我的钱包");
		save.setVisibility(View.VISIBLE);
		save.setText("设置密码");
		save.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
		password = SPUtils.get(this, null, SPContants.PASSWORD, "") + "";
		refresh();
	}

	public void refresh() {
		uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
		userPresenter = new UserPresenter(this);
		userPresenter.myWallet(uid);
	}

	@Override
	protected void onResume() {
		super.onResume();
		userPresenter.myWallet(uid);
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == UserPresenter.wallet_success) {
				MyWalletBean myWalletBean = (MyWalletBean) entity.getData();
				qianbao_money.setText(Double.parseDouble(myWalletBean
						.getBalance()+"") / 100 + "元");
			} else if (entity.getEventType() == UserPresenter.wallet_success) {
				ByAlert.alert(entity.getData().toString());
			}
		}
	}

}
